﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.IO;

using Newtonsoft.Json;
using SH.STD.Model;
using Cmn;
using SH.STD.DataProvider;
using System.Web.SessionState;

namespace SH.STD.Web.Handler
{
    /// <summary>
    /// Summary description for AchievementHandler
    /// </summary>
    public class AchievementHandler : IHttpHandler, IRequiresSessionState
    {
        public void ProcessRequest(HttpContext context)
        {
            context.Response.Cache.SetNoStore();
            context.Response.ContentType = "application/json;charset=gb2312";

            switch (context.Request.Params["action"])
            {
                case "loadData":
                    context.Response.Write(ConvertToJsonString(context.Request.Params["id"]));
                    break;
                case "addData":
                    context.Response.Write(AddData(context.Request.Params["jsonDataString"]));
                    break;
                case "removeData":
                    context.Response.Write(RemoveData(context.Request.Params["id"]));
                    break;
                case "updateData":
                    context.Response.Write(UpdateData(context.Request.Params["jsonDataString"]));
                    break;
            }
        }

        private string AddData(string jsonDataString)
        {
            JavaScriptObject achievement = (JavaScriptObject)JavaScriptConvert.DeserializeObject(jsonDataString);
            var sql = @"
INSERT INTO Achievement ([RegNo],[CertNo],[Name],[EvaluateDate],ProjectID,[DepID],RecommendDepID,[Introduce],AchievementIndex,ApplyPlace,Apply,[Memo],[LastModifiedBy])
            VALUES (@RegNo,@CertNo,@Name,@EvaluateDate,@ProjectID,@DepID,@RecommendDepID,@Introduce,@AchievementIndex,@ApplyPlace,@Apply,@Memo,@LastModifiedBy);SELECT @@identity";
            var cmdParas = new SqlParameter[]
                    {
                        new SqlParameter("RegNo",RepsonseInfo.Decode(achievement["RegNo"].ToString())),
                        new SqlParameter("CertNo",RepsonseInfo.Decode(achievement["CertNo"].ToString())),
                        new SqlParameter("Name",RepsonseInfo.Decode(achievement["Name"].ToString())),
                        new SqlParameter("EvaluateDate",achievement["EvaluateDate"]),
                        new SqlParameter("ProjectID",RepsonseInfo.Decode(achievement["ProjectID"].ToString())),
                        new SqlParameter("DepID",RepsonseInfo.Decode(achievement["DepID"].ToString())),
                        new SqlParameter("RecommendDepID",RepsonseInfo.Decode(achievement["RecommendDepID"].ToString())),
                        new SqlParameter("Introduce",RepsonseInfo.Decode(achievement["Introduce"].ToString())),
                        new SqlParameter("AchievementIndex",RepsonseInfo.Decode(achievement["AchievementIndex"].ToString())),
                        new SqlParameter("ApplyPlace",RepsonseInfo.Decode(achievement["ApplyPlace"].ToString())),
                        new SqlParameter("Apply",RepsonseInfo.Decode(achievement["Apply"].ToString())),
                        new SqlParameter("Memo",RepsonseInfo.Decode(achievement["Memo"].ToString())),
                        new SqlParameter("LastModifiedBy",Cmn.Session.GetUserID()),
                    };
            var ID = DB.getFieldValue(sql, cmdParas);
            return JavaScriptConvert.SerializeObject(new RepsonseInfo { MSG = ID });
        }
        private string ConvertToJsonString(string id)
        {
            DataTable dt = null;
            if (!String.IsNullOrEmpty(id))
            {
                var sql = @"
SELECT *
FROM [Achievement] 
WHERE ID=@ID";
                var cmdParams = new SqlParameter[]
                {
                    new SqlParameter("ID",id.ToInt()),
                };
                dt = Cmn.DB.getDataTable(sql, cmdParams);
            }
            else
            {
                var sql = @"
SELECT *
FROM [Achievement] 
WHERE Status=1";
                dt = Cmn.DB.getDataTable(sql);
            }

            return DataAccess.ConvertToJSON(dt, "Achievements");
        }
        private string UpdateData(string jsonDataString)
        {
            JavaScriptObject achievement = (JavaScriptObject)JavaScriptConvert.DeserializeObject(jsonDataString);
            var sql = @"
Update Achievement Set 
RegNo=@RegNo,
CertNo=@CertNo,
Name=@Name,
EvaluateDate=@EvaluateDate,
ProjectID=@ProjectID,
DepID=@DepID,
RecommendDepID=@RecommendDepID,
Introduce=@Introduce,
AchievementIndex=@AchievementIndex,
ApplyPlace=@ApplyPlace,
Apply=@Apply,
Memo=@Memo,
LastModifiedBy=@LastModifiedBy
WHERE ID=@ID";
            var cmdParas = new SqlParameter[]
                    {
                        new SqlParameter("ID",RepsonseInfo.Decode(achievement["ID"].ToString())),
                        new SqlParameter("RegNo",RepsonseInfo.Decode(achievement["RegNo"].ToString())),
                        new SqlParameter("CertNo",RepsonseInfo.Decode(achievement["CertNo"].ToString())),
                        new SqlParameter("Name",RepsonseInfo.Decode(achievement["Name"].ToString())),
                        new SqlParameter("EvaluateDate",RepsonseInfo.Decode(achievement["EvaluateDate"].ToString())),
                        new SqlParameter("ProjectID",RepsonseInfo.Decode(achievement["ProjectID"].ToString())),
                        new SqlParameter("DepID",RepsonseInfo.Decode(achievement["DepID"].ToString())),
                        new SqlParameter("RecommendDepID",RepsonseInfo.Decode(achievement["RecommendDepID"].ToString())),
                        new SqlParameter("Introduce",RepsonseInfo.Decode(achievement["Introduce"].ToString())),
                        new SqlParameter("AchievementIndex",RepsonseInfo.Decode(achievement["AchievementIndex"].ToString())),
                        new SqlParameter("ApplyPlace",RepsonseInfo.Decode(achievement["ApplyPlace"].ToString())),
                        new SqlParameter("Apply",RepsonseInfo.Decode(achievement["Apply"].ToString())),
                        new SqlParameter("Memo",RepsonseInfo.Decode(achievement["Memo"].ToString())),
                        new SqlParameter("LastModifiedBy",Cmn.Session.GetUserID()),
                    };
            var ret = DB.execSql(sql, cmdParas);
            return JavaScriptConvert.SerializeObject(new RepsonseInfo { MSG = RepsonseInfo.Decode(achievement["ID"].ToString()) });
        }

        private string RemoveData(string jsonDataString)
        {
            JavaScriptObject item = (JavaScriptObject)JavaScriptConvert.DeserializeObject(jsonDataString);
            var cmdParas = new SqlParameter[]
                    {
                        new SqlParameter("ID",RepsonseInfo.Decode(item["ID"].ToString())),
                    };
            var sql = "update Achievement set status=0 where ID=@ID";
            Cmn.DB.execSql(sql, cmdParas);

            return JavaScriptConvert.SerializeObject(new RepsonseInfo { MSG = item["ID"].ToString() });
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}